/**
 * 
 */
package spitfire.ksim.algorithm;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import spitfire.ksim.data.KNodeSnapshot;
import spitfire.ksim.data.KSensorSnapshot;

/**
 * @author Adam
 *
 */
public class RepresentativeSelection implements RequestAlgorithm {
	
	private LazyClassificationAlgorithm algo;
	
	public RepresentativeSelection(LazyClassificationAlgorithm algo) {
		this.algo = algo;
	}
	/**
	 * @see RequestAlgorithm#requestForSD(FuzzyRuleBase, KNodeSnapshot)
	 */
	@Override
	public String requestForSD(FuzzyRuleBase ruleBase, KNodeSnapshot snapshot) {
		System.out.println("Requesting with Representative");
		String maxSD = null;
		double scoreMax = 0;
		
		Map<String, KSensorSnapshot> typeSnrSnapShotMap = snapshot.getSensorSnapshotMap();
		// For each sensor type
		for (Entry<String, KSensorSnapshot> entry : typeSnrSnapShotMap.entrySet()) {
			String sensorType = entry.getKey();
//			List<Long> timeStampList = entry.getValue().getTimeStampList();
			List<Double> dataList = entry.getValue().getDataList();
			if ( ! ruleBase.containsSensorType(sensorType)) {
				// no such type category
				continue;
			}
			List<FuzzyEntry> feList = ruleBase.getFuzzyEntryListOf(sensorType);
			
			// Check data adequacy
//			if (!PreprocAndValidationAlgorithm.procAndValid(timeStampList, dataList)) {
//				System.out.println("Not enough data for requesting");
//				continue;
//			}
			
			System.out.println("Calculate score for <" + sensorType + ">");

			String sd = algo.classifySD(dataList, feList);
			double sc = algo.getSimilarity();
			System.out.println("Similarity to <" + sd + ">: " + sc);
			if (sc > scoreMax) {
				scoreMax = sc;
				maxSD = sd;
			}
		}
		
		if (maxSD == null) {
			System.out.println("No SD selected");
		} else {
			System.out.println("<" + maxSD + "> selected with score: " + scoreMax);
		}
		return maxSD;
	}
}
